<?php
	add_action('admin_footer', 'update_outlink');
	function update_outlink()
	{
        global $post;
        if($post->post_type == 'store')
        {
?>
    <script type="text/javascript">
    jQuery(document).ready(function($) {
        // Function to set deley
        var delay = (function(){
            var timer = 0;
            return function(callback, ms){
            clearTimeout (timer);
            timer = setTimeout(callback, ms);
            };
        })();
        // Event text change
        var store_aff_link = "#aff_url_store_metadata";
        if($(store_aff_link).length > 0)
        {
            $(store_aff_link).keyup(function(){
                
                delay(function(){
                    
                    var new_aff_link = $(store_aff_link).val();                    
                    var pID = $('#post_ID').val();
                    var type = 'store';
                    
                    values = new Array();
                    values[0] = pID;
                    values[1] = type;
                    values[2] = new_aff_link;
                    
                    var data = {
            		action: 'update_store_aff_link',
            		value_send: values
                    };
                    
                    $.post(ajaxurl, data, function(result) {
                        //alert(result);                            
                    })
                    
                }, 1000) // miliseconds
                
            })
        }
        
    })
    </script>
<?php
        }        
	}
    /**
     * FUNC CALLBACK UPDATE STORE AFF OUT LINK
     */
    add_action('wp_ajax_update_store_aff_link', 'update_store_aff_link_cb');
    function update_store_aff_link_cb()
    {
        $arr_values = $_POST['value_send'];
        $pID = $arr_values[0];
        $pType = $arr_values[1];
        $new_aff_link = $arr_values[2];
        
        if(outlink_DB_checkexist($pID,$pType) > 0)
        {
            $array = array('id'=>$pID, 'type'=>$pType, 'target'=>$new_aff_link);
            outlink_DB_update($array);
        }
        else
        {
            $mask = 's'.$pID;
            $array = array('id'=>$pID,'type'=>$pType,'mask'=>$mask,'target'=>$new_aff_link);
            outlink_DB_add($array);
        }
        die();
    }
    /**
     * FUNC CALLBACK UPDATE COUPON AFF OUT LINK
     */
    add_action('wp_ajax_update_coupon_aff_link', 'update_coupon_aff_link_cb');
    function update_coupon_aff_link_cb()
    {
        $arr_values = $_POST['value_send'];
        $pID = $arr_values[0];
        $pType = $arr_values[1];
        $new_aff_link = $arr_values[2];

        if(outlink_DB_checkexist($pID,$pType) > 0)
        {
            $array = array('id'=>$pID, 'type'=>$pType, 'target'=>$new_aff_link);
            outlink_DB_update($array);
        }
        else
        {
            $mask = 'c'.$pID;
            $array = array('id'=>$pID,'type'=>$pType,'mask'=>$mask,'target'=>$new_aff_link);
            outlink_DB_add($array);
            
            echo site_url().'/'.get_option('aff_out_link_prefix').'/'.$mask;
        }
        die();
    }
    /**
     * DELETE OUTLINK WHEN DELETE POST
     */
    
	add_action('publish_to_trash', 'before_delete_store_or_coupon');
	add_action('draft_to_trash', 'before_delete_store_or_coupon');
	add_action('future_to_trash', 'before_delete_store_or_coupon');
	function before_delete_store_or_coupon($post_id)
	{
        $ids = $_GET['post'];
        $type = $_GET['post_type'];
        $action = $_GET['action'];
        outlink_DB_delete($ids, $type);
	}
    